package com.mvp.lt.airlineview.utils;

import com.amap.api.maps.AMap;
import com.amap.api.maps.model.Circle;
import com.amap.api.maps.model.CircleOptions;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.Polygon;
import com.amap.api.maps.model.PolygonOptions;
import com.amap.api.maps.model.Polyline;
import com.amap.api.maps.model.PolylineOptions;
import com.mvp.lt.airlineview.R;
import com.mvp.lt.airlineview.map.MapCalculationLogLatDistance;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by MapShadowUtils.
 * User: Administrator
 * Name: AirLineView
 * functiona:带状航线
 * Date: 2019/10/16 0016
 * Time: 下午 17:24
 */
public class StripLineUtil2 {
    private AMap m3DAMap;
    private Polyline mPolyline2 = null;
    private double width;
    private List<Marker> mMarkerList = new ArrayList<>();
    private List<LatLng> latLngs = new ArrayList<>();
    private List<LatLng> mLatLngs = new ArrayList<>();
    public Polygon mRangePolygon;

    public void clrarMap() {
        if (latLngs != null) {
            latLngs.clear();
        }
        if (mLatLngs != null) {
            mLatLngs.clear();
        }

        if (mPolyline2 != null) {
            mPolyline2.remove();
        }
        if (mRangePolygon != null) {
            mRangePolygon.remove();
        }
        if (mPolyline2 != null) {
            mPolyline2.remove();
        }
        for (int i = 0; i < mMarkerList.size(); i++) {
            mMarkerList.get(i).destroy();
        }
        if (mMarkerList != null) {
            mMarkerList.clear();
        }

    }

    public StripLineUtil2(AMap m3DAMap, double width) {
        this.m3DAMap = m3DAMap;
        this.width = width;
    }


    public void setWidth(int width) {
        this.width = width;
    }

    /**
     * @param latLng
     */
    public void addLnglatLineWaypointMap(LatLng latLng) {
        mLatLngs.add(latLng);
        if (mPolyline2 != null) {
            mPolyline2.remove();
        }
        mMarkerList.add(getMarker(latLng));
        mPolyline2 = drawPolyLineOptions(m3DAMap, mLatLngs, 5, 0xFFFF0000);
        if (mLatLngs.size() > 2) {
            LatLng latLng0 = mLatLngs.get(mLatLngs.size() - 3);
            LatLng latLng1 = mLatLngs.get(mLatLngs.size() - 2);
            LatLng latLng2 = mLatLngs.get(mLatLngs.size() - 1);
            //计算带状航线内角
            int minDegree = (int) MapCalculationLogLatDistance.getTriangleAngle(latLng0.latitude, latLng0.longitude, latLng1.latitude, latLng1.longitude, latLng2.latitude, latLng2.longitude);
            int maxDegree = 90;
            if (minDegree == 180 || minDegree == 0) {
                minDegree = 90;
                maxDegree = -90;
            } else {
                minDegree = minDegree / 2;
                maxDegree = 180 - minDegree;
            }
            //两个点算出 获取角度
            int startAngle = (int) MapCalculationLogLatDistance.getAzimuthAngle(latLng0.latitude, latLng0.longitude, latLng1.latitude, latLng1.longitude);
            double linelength;
            if (minDegree < 20) {
                linelength = width;
            } else {
                linelength = MapCalculationLogLatDistance.cacleHypotenuseLong(minDegree, width);
            }
            //计算带状航线长度

            LogUtil.e("角度", startAngle + "------" + minDegree + "---" + maxDegree);
            int endAngle = (int) MapCalculationLogLatDistance.getAzimuthAngle(latLng1.latitude, latLng1.longitude, latLng2.latitude, latLng2.longitude);
            //   178------58---122
            LogUtil.e("角度", endAngle + "------" + minDegree + "---" + maxDegree);
            int l1;//300?
            int r1;//120
            List<LatLng> latLngsline = new ArrayList<>();
            LatLng latLonPointleft2;
            LatLng latLonPointright2;
            if (endAngle - startAngle > 0 && Math.abs(endAngle - startAngle) <= 180) {//左拐

                r1 = startAngle + (180 - minDegree);//300?
                l1 = startAngle - (180 - maxDegree);//120
                LatLng latLonPointleft1 = MapCalculationLogLatDistance.convertDistanceToLogLat(latLng1.longitude, latLng1.latitude, linelength, l1);
                LatLng latLonPointright1 = MapCalculationLogLatDistance.convertDistanceToLogLat(latLng1.longitude, latLng1.latitude, linelength, r1);

                latLonPointleft2 = MapCalculationLogLatDistance.convertDistanceToLogLat(latLng2.longitude, latLng2.latitude, width, endAngle - 90);
                latLonPointright2 = MapCalculationLogLatDistance.convertDistanceToLogLat(latLng2.longitude, latLng2.latitude, width, endAngle + 90);

                latLngsline.add(0, new LatLng(latLonPointleft1.latitude, latLonPointleft1.longitude));
                latLngsline.add( new LatLng(latLonPointright1.latitude, latLonPointright1.longitude));


                latLngs.set(0, new LatLng(latLonPointleft1.latitude, latLonPointleft1.longitude));
                latLngs.set(latLngs.size() - 1, new LatLng(latLonPointright1.latitude, latLonPointright1.longitude));

            } else if (endAngle - startAngle < 0 && Math.abs(endAngle - startAngle) > 180) {//左拐
                r1 = startAngle + (180 - minDegree);//300?
                l1 = startAngle - (180 - maxDegree);//120
                LatLng latLonPointleft1 = MapCalculationLogLatDistance.convertDistanceToLogLat(latLng1.longitude, latLng1.latitude, linelength, l1);
                LatLng latLonPointright1 = MapCalculationLogLatDistance.convertDistanceToLogLat(latLng1.longitude, latLng1.latitude, linelength, r1);

                latLonPointleft2 = MapCalculationLogLatDistance.convertDistanceToLogLat(latLng2.longitude, latLng2.latitude, width, endAngle - 90);
                latLonPointright2 = MapCalculationLogLatDistance.convertDistanceToLogLat(latLng2.longitude, latLng2.latitude, width, endAngle + 90);

                latLngsline.add(0, new LatLng(latLonPointleft1.latitude, latLonPointleft1.longitude));
                latLngsline.add(new LatLng(latLonPointright1.latitude, latLonPointright1.longitude));

                latLngs.set(0, new LatLng(latLonPointleft1.latitude, latLonPointleft1.longitude));
                latLngs.set(latLngs.size() - 1, new LatLng(latLonPointright1.latitude, latLonPointright1.longitude));
            } else {  //右拐
                l1 = startAngle + (180 - maxDegree);//300?
                r1 = startAngle - (180 - minDegree);//120
                LatLng latLonPointleft1 = MapCalculationLogLatDistance.convertDistanceToLogLat(latLng1.longitude, latLng1.latitude, linelength, l1);
                LatLng latLonPointright1 = MapCalculationLogLatDistance.convertDistanceToLogLat(latLng1.longitude, latLng1.latitude, linelength, r1);

                latLonPointleft2 = MapCalculationLogLatDistance.convertDistanceToLogLat(latLng2.longitude, latLng2.latitude, width, endAngle - 90);
                latLonPointright2 = MapCalculationLogLatDistance.convertDistanceToLogLat(latLng2.longitude, latLng2.latitude, width, endAngle + 90);

                latLngsline.add(0, new LatLng(latLonPointright1.latitude, latLonPointright1.longitude));
                latLngsline.add( new LatLng(latLonPointleft1.latitude, latLonPointleft1.longitude));

                latLngs.set(0, new LatLng(latLonPointright1.latitude, latLonPointright1.longitude));
                latLngs.set(latLngs.size() - 1, new LatLng(latLonPointleft1.latitude, latLonPointleft1.longitude));
            }

            latLngsline.add(0, new LatLng(latLonPointleft2.latitude, latLonPointleft2.longitude));
            latLngsline.add(new LatLng(latLonPointright2.latitude, latLonPointright2.longitude));

            latLngs.add(0, new LatLng(latLonPointleft2.latitude, latLonPointleft2.longitude));
            latLngs.add(new LatLng(latLonPointright2.latitude, latLonPointright2.longitude));

            drawPolygonOptionsBlue(latLngsline);
        } else if (mLatLngs.size() == 2) {
            List<LatLng> latLngsline = new ArrayList<>();
            LatLng latLng1 = mLatLngs.get(mLatLngs.size() - 2);
            LatLng latLng2 = mLatLngs.get(mLatLngs.size() - 1);
            //获取角度
            double angle = MapCalculationLogLatDistance.getAzimuthAngle(latLng1.latitude, latLng1.longitude, latLng2.latitude, latLng2.longitude);
            LogUtil.e("角度", angle + "");
            //距离
            LatLng latLonPointleft1 = MapCalculationLogLatDistance.convertDistanceToLogLat(latLng1.longitude, latLng1.latitude, width, angle + 90);

            LatLng latLonPointright1 = MapCalculationLogLatDistance.convertDistanceToLogLat(latLng1.longitude, latLng1.latitude, width, angle - 90);

            LatLng latLonPointleft2 = MapCalculationLogLatDistance.convertDistanceToLogLat(latLng2.longitude, latLng2.latitude, width, angle + 90);

            LatLng latLonPointright2 = MapCalculationLogLatDistance.convertDistanceToLogLat(latLng2.longitude, latLng2.latitude, width, angle - 90);

            latLngsline.add(0, new LatLng(latLonPointright1.latitude, latLonPointright1.longitude));
            latLngsline.add(new LatLng(latLonPointleft1.latitude, latLonPointleft1.longitude));
            latLngsline.add(0, new LatLng(latLonPointright2.latitude, latLonPointright2.longitude));
            latLngsline.add(new LatLng(latLonPointleft2.latitude, latLonPointleft2.longitude));

            latLngs.add(0, new LatLng(latLonPointright1.latitude, latLonPointright1.longitude));
            latLngs.add(new LatLng(latLonPointleft1.latitude, latLonPointleft1.longitude));

            latLngs.add(0, new LatLng(latLonPointright2.latitude, latLonPointright2.longitude));
            latLngs.add(new LatLng(latLonPointleft2.latitude, latLonPointleft2.longitude));
            drawPolygonOptionsBlue(latLngsline);
        }
        if (mRangePolygon != null) {
            mRangePolygon.remove();
        }
        mRangePolygon = drawPolygonOptions(latLngs);

    }

    private Polygon drawPolygonOptionsBlue(List<LatLng> linelatLngs) {
        PolygonOptions polygonOptions = new PolygonOptions();
        polygonOptions.addAll(linelatLngs);
        polygonOptions.strokeWidth(5) // 多边形的边框
                .strokeColor(AndroidUtil.getColor(R.color.deepskyblue)) // 边框颜色
                .fillColor(0x00000000);   // 多边形的填充色
        return m3DAMap.addPolygon(polygonOptions);
    }

    private Polygon drawPolygonOptions(List<LatLng> linelatLngs) {
        PolygonOptions polygonOptions = new PolygonOptions();
        polygonOptions.addAll(linelatLngs);
        polygonOptions.strokeWidth(0) // 多边形的边框
                .strokeColor(0x33000000) // 边框颜色
                .fillColor(0x33000000);   // 多边形的填充色
        return m3DAMap.addPolygon(polygonOptions);
    }

    Circle startCircle;
    Circle endCircle;

    /**
     * @param centerpoint 中心点坐标
     * @param radius      半径 1公里(km)=1000米(m)
     */
    public void addPolylinesScircle(LatLng centerpoint, int radius) {
        if (startCircle == null) {
            startCircle = m3DAMap.addCircle(new CircleOptions()
                    .strokeColor(0x33000000)
                    .fillColor(0x33000000)
                    .strokeWidth(0));
        }

        startCircle.setRadius(radius - radius * 0.1);

        startCircle.setCenter(centerpoint);
    }

    /**
     * @param centerpoint 中心点坐标
     * @param radius      半径 1公里(km)=1000米(m)
     */
    public void addPolylinesEcircle(LatLng centerpoint, int radius) {
        if (endCircle == null) {
            endCircle = m3DAMap.addCircle(new CircleOptions()
                    .strokeColor(0x33000000)
                    .fillColor(0x33000000)
                    .strokeWidth(0));
        }

        endCircle.setRadius(radius - radius * 0.1);
        endCircle.setCenter(centerpoint);
    }

    private Marker getMarker(LatLng latLng) {
        final MarkerOptions markerOptions = new MarkerOptions().position(latLng).zIndex(2);
        Marker marker = m3DAMap.addMarker(markerOptions);
        marker.setAnchor(0.5f, 1f);
        marker.setDraggable(true);

        return marker;
    }

    public Polyline drawPolyLineOptions(AMap aMap, List<LatLng> linelatLngs,
                                        int w, int color) {
        PolylineOptions polylineOptions = new PolylineOptions();
        polylineOptions.addAll(linelatLngs);
        polylineOptions.width(w) // 多边形的边框
                .color(color); // 边框颜色
        return aMap.addPolyline(polylineOptions);
    }

    //一段线

    public Polyline drawOneLenthPolyline(AMap amap, List<Marker> markerList, int w, int color) {
        List<LatLng> points = new ArrayList<LatLng>();
        PolylineOptions opts = new PolylineOptions();
        try {
            if (markerList == null) {
                return null;
            } else if (markerList.size() > 1) {
                Marker marker0 = markerList.get(markerList.size() - 2);
                Marker marker1 = markerList.get(markerList.size() - 1);
                points.add(marker0.getPosition());
                points.add(marker1.getPosition());
                opts.addAll(points);
                opts.width(w);
                opts.color(color);
                opts.zIndex(1);
                return amap.addPolyline(opts);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }    //一段线

    public Polyline drawOneLenthPolyline(AMap amap, LatLng latLng1, LatLng latLng2) {
        List<LatLng> points = new ArrayList<LatLng>();
        PolylineOptions opts = new PolylineOptions();
        points.add(latLng1);
        points.add(latLng2);
        opts.addAll(points);
        opts.width(10);
        opts.color(0x59000000);
        opts.zIndex(1);
        return amap.addPolyline(opts);
    }


}
